/** 
 * PrimalWindowTitle.java
 * @author Lex
 * 2007-9-8 ����09:43:39
 */
package lex.title.basic;

import java.awt.*;
import java.util.List;
import javax.swing.*;

public abstract class PrimalWindowTitle extends JComponent {
	public static final int IMAGE_WIDTH = 16;
	public static final int IMAGE_HEIGHT = 16;
	private int titleExtraHeight = 7;
	// ========================================
	private JRootPane rootPane = null;
	protected Window window = null;

	public PrimalWindowTitle(JRootPane rootPane) {
		super();
		this.rootPane = rootPane;
		window = SwingUtilities.getWindowAncestor(rootPane);
		Font font = UIManager.getFont("InternalFrame.titleFont");
		this.setFont(font);
		rootPane.setFont(font);
	}

	public JRootPane getRootPane() {
		return rootPane;
	}

	public Dimension getPreferredSize() {
		Dimension size = super.getPreferredSize();
		FontMetrics fm = rootPane.getFontMetrics(rootPane.getFont());
		int fontHeight = fm.getHeight();
		fontHeight += titleExtraHeight;
		int iconHeight = 0;
		Image image = getIconImage();
		if (null != image)
			iconHeight = image.getHeight(this);
		int finalHeight = Math.max(fontHeight, iconHeight);
		size.height = finalHeight;
		return size;
	}

	protected void setTitleExtraHeight(int height) {
		this.titleExtraHeight = height;
	}

	public Window getWindow() {
		return this.window;
	}

	public Image getIconImage()
	{
		//null
		if( null == window )
			return null;
		//frame
		if( isFrame() )
		{
			List<Image> icons = this.window.getIconImages();
			if( null != icons && icons.size() > 0 )
				return icons.get( 0 );
			return null;
		}
		//dialog
		else
		{
			Frame root = (Frame) SwingUtilities.getAncestorOfClass(
				Frame.class, window );
			if( null == root )
				return null;
			else
				return root.getIconImage();
		}
	}

	public int getWindowDecorationStyle() {
		return getRootPane().getWindowDecorationStyle();
	}

	public boolean isNoneDecoration() {
		return rootPane.getWindowDecorationStyle() == JRootPane.NONE;
	}

	public boolean isFrame() {
		return rootPane.getWindowDecorationStyle() == JRootPane.FRAME;
	}

	public boolean isDialog() {
		return isPlainDialog() || isInformationDialog() || isErrorDialog() || isColorChooserDialog() || isFileChooserDialog() || isQuestionDialog()
				|| isWarningDialog();
	}

	public boolean isPlainDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.PLAIN_DIALOG;
	}

	public boolean isInformationDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.INFORMATION_DIALOG;
	}

	public boolean isErrorDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.ERROR_DIALOG;
	}

	public boolean isColorChooserDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.COLOR_CHOOSER_DIALOG;
	}

	public boolean isFileChooserDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.FILE_CHOOSER_DIALOG;
	}

	public boolean isQuestionDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.QUESTION_DIALOG;
	}

	public boolean isWarningDialog() {
		return rootPane.getWindowDecorationStyle() == JRootPane.WARNING_DIALOG;
	}

	public Frame getFrame() {
		if (window instanceof Frame) {
			return (Frame) window;
		}
		return null;
	}

	public String getTitle() {
		if (window instanceof Frame) {
			return ((Frame) window).getTitle();
		} else if (window instanceof Dialog) {
			return ((Dialog) window).getTitle();
		}
		return null;
	}

	public boolean isLeftToRight() {
		Component c = window;
		if (null == window)
			c = rootPane;
		return c.getComponentOrientation().isLeftToRight();
	}

	protected boolean isComponentAdded(Component c) {
		return (null != c) && (null != c.getParent());
	}
}
